<template>
<div>
     <Modal v-model="modal" :title="title" :styles="{width:'600px',top:'50px'}">
        <Form ref='modifyForm' :model="obj" label-position="right" :rules="ruleValidate" :label-width="150" >
            <FormItem label="进项发票编码" prop="income_invoice_code">
                <Input  v-model="obj.income_invoice_code" style="width:330px;" disabled></Input>
            </FormItem>
            <FormItem label="进项发票号码" prop="income_invoice_no">
                <Input  v-model="obj.income_invoice_no" style="width:330px;" disabled></Input>
            </FormItem>
            <FormItem label="买入方名称" prop="buyer_com_name">
                <Input  v-model="obj.buyer_com_name" style="width:330px;" disabled></Input>
            </FormItem>
            <FormItem label="本次使用数量" prop="invoice_amount">
                <Input v-model="obj.invoice_amount" style="width:330px;"></Input>
            </FormItem>
           

         </Form>
        
         <div slot="footer">
             <Button @click="cancel()">关闭</Button>
             <Button type="primary" @click="submit('add')" >
             添加</Button>
         </div>
     </Modal>
</div>
</template>
<script>
 export default {
     data (){
         return {
             modal:false,title:'添加',
             obj:{income_invoice_code:'',invoice_guid:'',income_invoice_no:'',buyer_com_name:'',invoice_amount:null,invoice_code:'',invoice_no:''},
             ruleValidate:{
                 // income_invoice_code:[
                 //     { required: true, message: '进项发票编码不能为空', trigger: 'blur' }
                 // ],income_invoice_no:[
                 //     {required:true,message:'进项发票号码不能为空',trigger:'blur'}
                 // ],buyer_com_name:[
                 //     {required:true,message:'买入方名称不能为空',trigger:'blur'}
                 // ],
                 invoice_amount:[
                     {required:true,message:'本次使用数量不能为空',trigger:'blur'},
                     {validator:(r,v,call)=>{
                        console.log(r,v)
                        if(Number(v)<=0){
                            call(new Error('数量需大于0'))
                        }else{
                            call();
                        }
                     },trigger:'blur'}
                 ]
             }
         }
     },methods:{
         showdata(params){
            console.log(params);
            
            this.obj.invoice_guid=params.guid;
            this.obj.income_invoice_no=params.invoice_no;
            this.obj.income_invoice_code=params.invoice_code;
            this.obj.buyer_com_name=params.buyer_name;
            //this.$refs['modifyForm'].resetFields();
            this.modal=true;
         },
         submit(type){
             if(this.handleSubmit()){

                 var url='/incomeinvoice/usedrecord/add';
                 
                 
                 this.axios.post(url,this.obj).then((res)=>{
                     console.log(res);
                     if(res.errorCode==0){
                        //成功
                         this.modal=false;
                         this.obj={};
                         this.$Message.success('成功');
                         this.$emit('refresh');
                     }else{
                        //this.$Message.error(res.message);
                     }
                 }) 
                 return ;  
                 
             }
         },
         cancel(){
             this.modal=false;
         },handleSubmit () {
             var a=true;
             this.$refs['modifyForm'].validate((valid) => {
                 if (valid) {
                     //this.$Message.success('Success!');
                 } else {
                     //this.$Message.error('Fail!');
                     a=false;
                 }
             })
             return a;
         },
     },created(){
         this.$bus.on('bus_makeupedit', this.showdata);
         
     },beforeDestroy(){
         this.$bus.off('bus_makeupedit',this.showdata)
     }
 }
</script>
